home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / base / models.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.4 KB  |  472 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from base.g import *
  5. from base import utils
  6. import os.path as os
  7. import re
  8. import glob
  9.  
  10. try:
  11.     import datetime
  12.     datetime_avail = True
  13. except ImportError:
  14.     datetime_avail = False
  15.     datetime = None
  16.  
  17. pat_prod_num = re.compile('(\\d+)', re.I)
  18. TYPE_UNKNOWN = 0
  19. TYPE_STRING = 1
  20. TYPE_STR = 1
  21. TYPE_LIST = 2
  22. TYPE_BOOL = 3
  23. TYPE_INT = 4
  24. TYPE_HEX = 5
  25. TYPE_BITFIELD = 6
  26. TYPE_URI = TYPE_STR
  27. TYPE_DATE = 8
  28. TECH_CLASSES = [
  29.     'Undefined',
  30.     'Unsupported',
  31.     'Postscript',
  32.     'DJGenericVIP',
  33.     'LJMono',
  34.     'LJColor',
  35.     'LJFastRaster',
  36.     'LJJetReady',
  37.     'DJ350',
  38.     'DJ540',
  39.     'DJ600',
  40.     'DJ6xx',
  41.     'DJ6xxPhoto',
  42.     'DJ630',
  43.     'DJ8xx',
  44.     'DJ8x5',
  45.     'DJ850',
  46.     'DJ890',
  47.     'DJ9xx',
  48.     'DJ9xxVIP',
  49.     'DJ3600',
  50.     'DJ3320',
  51.     'DJ4100',
  52.     'AP2xxx',
  53.     'AP21xx',
  54.     'AP2560',
  55.     'PSP100',
  56.     'PSP470',
  57.     'LJZjsMono',
  58.     'LJZjsColor',
  59.     'LJm1005',
  60.     'QuickConnect',
  61.     'DJ55xx',
  62.     'OJProKx50',
  63.     'LJP1XXX']
  64. TECH_CLASSES.sort()
  65. TECH_CLASS_PDLS = {
  66.     'Postscript': 'ps',
  67.     'DJGenericVIP': 'pcl3',
  68.     'LJMono': 'pcl3',
  69.     'LJColor': 'pcl3',
  70.     'LJFastRaster': 'pclxl',
  71.     'LJJetReady': 'pclxl',
  72.     'DJ350': 'pcl3',
  73.     'DJ540': 'pcl3',
  74.     'DJ600': 'pcl3',
  75.     'DJ6xx': 'pcl3',
  76.     'DJ6xxPhoto': 'pcl3',
  77.     'DJ630': 'pcl3',
  78.     'DJ8xx': 'pcl3',
  79.     'DJ8x5': 'pcl3',
  80.     'DJ850': 'pcl3',
  81.     'DJ890': 'pcl3',
  82.     'DJ9xx': 'pcl3',
  83.     'DJ9xxVIP': 'pcl3',
  84.     'DJ3600': 'lidil',
  85.     'DJ3320': 'lidil',
  86.     'DJ4100': 'lidil',
  87.     'AP2xxx': 'pcl3',
  88.     'AP21xx': 'pcl3',
  89.     'AP2560': 'pcl3',
  90.     'PSP100': 'pcl3',
  91.     'PSP470': 'pcl3',
  92.     'LJZjsMono': 'zjs',
  93.     'LJZjsColor': 'zjs',
  94.     'LJm1005': 'zxs',
  95.     'QuickConnect': 'jpeg',
  96.     'DJ55xx': 'pcl3',
  97.     'OJProKx50': 'pcl3',
  98.     'LJP1XXX': 'zxs' }
  99. PDL_TYPE_PCL = 0
  100. PDL_TYPE_PS = 1
  101. PDL_TYPE_HOST = 2
  102. PDL_TYPES = {
  103.     'pcl3': PDL_TYPE_PCL,
  104.     'pcl5': PDL_TYPE_PCL,
  105.     'pcl6': PDL_TYPE_PCL,
  106.     'pcl5e': PDL_TYPE_PCL,
  107.     'pcl': PDL_TYPE_PCL,
  108.     'pclxl': PDL_TYPE_PCL,
  109.     'ps': PDL_TYPE_PS,
  110.     'lidil': PDL_TYPE_HOST,
  111.     'zjs': PDL_TYPE_HOST,
  112.     'zjstream': PDL_TYPE_HOST,
  113.     'zxs': PDL_TYPE_HOST,
  114.     'zxstream': PDL_TYPE_HOST,
  115.     'jpeg': PDL_TYPE_HOST,
  116.     'jpg': PDL_TYPE_HOST,
  117.     'jetready': PDL_TYPE_HOST,
  118.     'jr': PDL_TYPE_HOST }
  119. TECH_SUBCLASSES = [
  120.     'LargeFormatSuperB',
  121.     'LargeFormatA3',
  122.     'CoverMedia',
  123.     'FullBleed',
  124.     'Duplex',
  125.     'Normal',
  126.     'Apollo2000',
  127.     'Apollo2200',
  128.     'Apollo2500',
  129.     'NoPhotoMode',
  130.     'NoPhotoBestHiresModes',
  131.     'No1200dpiNoSensor',
  132.     'NoFullBleed',
  133.     '4x6FullBleed',
  134.     '300dpiOnly',
  135.     'GrayscaleOnly']
  136. TECH_SUBCLASSES.sort()
  137. MODEL_UI_REPLACEMENTS = {
  138.     'laserjet': 'LaserJet',
  139.     'psc': 'PSC',
  140.     'hp': 'HP',
  141.     'mfp': 'MFP' }
  142.  
  143. def normalizeModelUIName(model):
  144.     ml = model.lower().strip()
  145.     if 'apollo' in ml:
  146.         z = ml.replace('_', ' ')
  147.     elif ml.startswith('hp'):
  148.         z = ml[3:].replace('_', ' ')
  149.     else:
  150.         z = ml.replace('_', ' ')
  151.     y = []
  152.     for x in z.split():
  153.         if pat_prod_num.search(x):
  154.             y.append(x)
  155.             continue
  156.         y.append(MODEL_UI_REPLACEMENTS.get(x, x.capitalize()))
  157.     
  158.     if 'apollo' in ml:
  159.         return ' '.join(y)
  160.     return 'HP ' + ' '.join(y)
  161.  
  162.  
  163. def normalizeModelName(model):
  164.     return utils.xstrip(model.replace(' ', '_').replace('__', '_').replace('~', '').replace('/', '_'), '_')
  165.  
  166.  
  167. class ModelData:
  168.     
  169.     def __init__(self, root_path = None):
  170.         if root_path is None:
  171.             self.root_path = prop.models_dir
  172.         else:
  173.             self.root_path = root_path
  174.         self._ModelData__cache = { }
  175.         self.reset_includes()
  176.         self.sec = re.compile('^\\[(.*)\\]')
  177.         self.inc = re.compile('^\\%include (.*)', re.I)
  178.         self.inc_line = re.compile('^\\%(.*)\\%')
  179.         self.eq = re.compile('^([^=]+)=(.*)')
  180.         self.date = re.compile('^(\\d{1,2})/(\\d{1,2})/(\\d{4,4})')
  181.         files = [
  182.             (os.path.join(self.root_path, 'models.dat'), os.path.join(self.root_path, 'unreleased', 'unreleased.dat')),
  183.             (os.path.join(os.getcwd(), 'data', 'models', 'models.dat'), os.path.join(os.getcwd(), 'data', 'models', 'unreleased', 'unreleased.dat'))]
  184.         for None in files:
  185.             (self.released_dat, self.unreleased_dat) = None
  186.             if os.path.exists(self.released_dat):
  187.                 break
  188.                 continue
  189.         else:
  190.             (self.released_dat, self.unreleased_dat) = (None, None)
  191.         self.FIELD_TYPES = {
  192.             'align-type': TYPE_INT,
  193.             'clean-type': TYPE_INT,
  194.             'color-cal-type': TYPE_INT,
  195.             'copy-type': TYPE_INT,
  196.             'embedded-server-type': TYPE_INT,
  197.             'fax-type': TYPE_INT,
  198.             'fw-download': TYPE_BOOL,
  199.             'icon': TYPE_STR,
  200.             'io-mfp-mode': TYPE_INT,
  201.             'io-mode': TYPE_INT,
  202.             'io-support': TYPE_BITFIELD,
  203.             'monitor-type': TYPE_INT,
  204.             'linefeed-cal-type': TYPE_INT,
  205.             'panel-check-type': TYPE_INT,
  206.             'pcard-type': TYPE_INT,
  207.             'plugin': TYPE_INT,
  208.             'plugin-reason': TYPE_BITFIELD,
  209.             'power-settings': TYPE_INT,
  210.             'pq-diag-type': TYPE_INT,
  211.             'r-type': TYPE_INT,
  212.             'scan-style': TYPE_INT,
  213.             'scan-type': TYPE_INT,
  214.             'status-battery-check': TYPE_INT,
  215.             'status-dynamic-counters': TYPE_INT,
  216.             'status-type': TYPE_INT,
  217.             'support-subtype': TYPE_HEX,
  218.             'support-released': TYPE_BOOL,
  219.             'support-type': TYPE_INT,
  220.             'support-ver': TYPE_STR,
  221.             'tech-class': TYPE_LIST,
  222.             'tech-subclass': TYPE_LIST,
  223.             'tech-type': TYPE_INT,
  224.             'usb-pid': TYPE_HEX,
  225.             'usb-vid': TYPE_HEX,
  226.             'job-storage': TYPE_INT,
  227.             'dev-file': TYPE_STR,
  228.             'fax-uri': TYPE_STR,
  229.             'scan-uri': TYPE_STR,
  230.             'is-hp': TYPE_BOOL,
  231.             'host': TYPE_STR,
  232.             'status-desc': TYPE_STR,
  233.             'cups-printer': TYPE_STR,
  234.             'serial': TYPE_STR,
  235.             'error-state': TYPE_INT,
  236.             'device-state': TYPE_INT,
  237.             'panel': TYPE_INT,
  238.             'device-uri': TYPE_STR,
  239.             'panel-line1': TYPE_STR,
  240.             'panel-line2': TYPE_STR,
  241.             'back-end': TYPE_STR,
  242.             'port': TYPE_INT,
  243.             'deviceid': TYPE_STR,
  244.             'cups-uri': TYPE_STR,
  245.             'status-code': TYPE_INT,
  246.             'rs': TYPE_STR,
  247.             'rr': TYPE_STR,
  248.             'rg': TYPE_STR,
  249.             'r': TYPE_INT,
  250.             'duplexer': TYPE_INT,
  251.             'supply-door': TYPE_INT,
  252.             'revision': TYPE_INT,
  253.             'media-path': TYPE_INT,
  254.             'top-door': TYPE_BOOL,
  255.             'photo-tray': TYPE_BOOL }
  256.         self.RE_FIELD_TYPES = {
  257.             re.compile('^r(\\d+)-agent(\\d+)-kind', re.IGNORECASE): TYPE_INT,
  258.             re.compile('^r(\\d+)-agent(\\d+)-type', re.IGNORECASE): TYPE_INT,
  259.             re.compile('^r(\\d+)-agent(\\d+)-sku', re.IGNORECASE): TYPE_STR,
  260.             re.compile('^agent(\\d+)-desc', re.IGNORECASE): TYPE_STR,
  261.             re.compile('^agent(\\d+)-virgin', re.IGNORECASE): TYPE_BOOL,
  262.             re.compile('^agent(\\d+)-dvc', re.IGNORECASE): TYPE_INT,
  263.             re.compile('^agent(\\d+)-kind', re.IGNORECASE): TYPE_INT,
  264.             re.compile('^agent(\\d+)-type', re.IGNORECASE): TYPE_INT,
  265.             re.compile('^agent(\\d+)-id', re.IGNORECASE): TYPE_INT,
  266.             re.compile('^agent(\\d+)-hp-ink', re.IGNORECASE): TYPE_BOOL,
  267.             re.compile('^agent(\\d+)-health-desc', re.IGNORECASE): TYPE_STR,
  268.             re.compile('^agent(\\d+)-health$', re.IGNORECASE): TYPE_INT,
  269.             re.compile('^agent(\\d+)-known', re.IGNORECASE): TYPE_BOOL,
  270.             re.compile('^agent(\\d+)-level', re.IGNORECASE): TYPE_INT,
  271.             re.compile('^agent(\\d+)-ack', re.IGNORECASE): TYPE_BOOL,
  272.             re.compile('^agent(\\d+)-sku', re.IGNORECASE): TYPE_STR,
  273.             re.compile('^in-tray(\\d+)', re.IGNORECASE): TYPE_BOOL,
  274.             re.compile('^out-tray(\\d+)', re.IGNORECASE): TYPE_BOOL,
  275.             re.compile('model(\\d+)', re.IGNORECASE): TYPE_STR }
  276.         self.TYPE_CACHE = { }
  277.  
  278.     
  279.     def read_all_files(self, unreleased = True):
  280.         if os.path.exists(self.released_dat):
  281.             self.read_section(self.released_dat)
  282.             if self.unreleased_dat is not None and os.path.exists(self.unreleased_dat):
  283.                 self.read_section(self.unreleased_dat)
  284.             
  285.         
  286.         return self._ModelData__cache
  287.  
  288.     
  289.     def read_section(self, filename, section = None, is_include = False):
  290.         found = False
  291.         in_section = False
  292.         if section is not None:
  293.             section = section.lower()
  294.             if is_include:
  295.                 log.debug('Searching for include [%s] in file %s' % (section, filename))
  296.             else:
  297.                 log.debug('Searching for section [%s] in file %s' % (section, filename))
  298.         
  299.         if is_include:
  300.             cache = self._ModelData__includes
  301.         else:
  302.             cache = self._ModelData__cache
  303.         
  304.         try:
  305.             fd = file(filename)
  306.         except IOError:
  307.             e = None
  308.             log.error('I/O Error: %s (%s)' % (filename, e.strerror))
  309.             return False
  310.  
  311.         while True:
  312.             line = fd.readline()
  313.             if not line:
  314.                 break
  315.             
  316.             if line[0] in ('#', ';'):
  317.                 continue
  318.             
  319.             if line[0] == '[':
  320.                 if in_section and section is not None:
  321.                     break
  322.                 
  323.                 match = self.sec.search(line)
  324.                 if match is not None:
  325.                     in_section = True
  326.                     read_section = match.group(1).lower()
  327.                     if section is not None:
  328.                         found = in_section = read_section == section
  329.                     
  330.                     if in_section:
  331.                         if section is not None:
  332.                             log.debug('Found section [%s] in file %s' % (read_section, filename))
  333.                         
  334.                         cache[read_section] = { }
  335.                     
  336.                 in_section
  337.                 continue
  338.             
  339.             if line[0] == '%':
  340.                 match = self.inc.match(line)
  341.                 if match is not None:
  342.                     inc_file = match.group(1)
  343.                     log.debug('Found include file directive: %%include %s' % inc_file)
  344.                     self._ModelData__include_files.append(os.path.join(os.path.dirname(filename), inc_file))
  345.                     continue
  346.                 
  347.                 if in_section:
  348.                     match = self.inc_line.match(line)
  349.                     if match is not None:
  350.                         inc_sect = match.group(1)
  351.                         log.debug('Found include directive %%%s%%' % inc_sect)
  352.                         
  353.                         try:
  354.                             self._ModelData__includes[inc_sect]
  355.                         except KeyError:
  356.                             for inc in self._ModelData__include_files:
  357.                                 if self.read_section(inc, inc_sect, True):
  358.                                     break
  359.                                     continue
  360.                             
  361.                         
  362.  
  363.                     None<EXCEPTION MATCH>KeyError
  364.                 
  365.             
  366.             if in_section:
  367.                 match = self.eq.search(line)
  368.                 if match is not None:
  369.                     key = match.group(1)
  370.                     value = match.group(2)
  371.                     value = self.convert_data(key, value)
  372.                     cache[read_section][key] = value
  373.                 
  374.             match is not None
  375.         fd.close()
  376.         return found
  377.  
  378.     
  379.     def reset_includes(self):
  380.         self._ModelData__include_files = []
  381.         self._ModelData__includes = { }
  382.  
  383.     
  384.     def __getitem__(self, model):
  385.         model = model.lower()
  386.         
  387.         try:
  388.             return self._ModelData__cache[model]
  389.         except:
  390.             log.debug('Cache miss: %s' % model)
  391.             log.debug('Reading file: %s' % self.released_dat)
  392.             if self.read_section(self.released_dat, model):
  393.                 return self._ModelData__cache[model]
  394.             return { }
  395.  
  396.  
  397.     
  398.     def all_models(self):
  399.         return self._ModelData__cache
  400.  
  401.     
  402.     def get_data_type(self, key):
  403.         
  404.         try:
  405.             return self.FIELD_TYPES[key]
  406.         except KeyError:
  407.             
  408.             try:
  409.                 return self.TYPE_CACHE[key]
  410.             except KeyError:
  411.                 for pat, typ in self.RE_FIELD_TYPES.items():
  412.                     match = pat.match(key)
  413.                     if match is not None:
  414.                         self.TYPE_CACHE[key] = typ
  415.                         return typ
  416.                 
  417.             except:
  418.                 match is not None
  419.             
  420.  
  421.             None<EXCEPTION MATCH>KeyError
  422.  
  423.         log.warn('get_data_type(): Defaulted to TYPE_STR for key %s' % key)
  424.         return TYPE_STR
  425.  
  426.     
  427.     def convert_data(self, key, value, typ = None):
  428.         if typ is None:
  429.             typ = self.get_data_type(key)
  430.         
  431.         if typ in (TYPE_BITFIELD, TYPE_INT):
  432.             
  433.             try:
  434.                 value = int(value)
  435.             except (ValueError, TypeError):
  436.                 log.error('Invalid value in .dat file: %s=%s' % (key, value))
  437.                 value = 0
  438.             except:
  439.                 None<EXCEPTION MATCH>(ValueError, TypeError)
  440.             
  441.  
  442.         None<EXCEPTION MATCH>(ValueError, TypeError)
  443.         if typ == TYPE_BOOL:
  444.             value = utils.to_bool(value)
  445.         elif typ == TYPE_LIST:
  446.             value = _[1]
  447.         elif typ == TYPE_DATE:
  448.             if datetime_avail:
  449.                 match = self.date.search(value)
  450.                 if match is not None:
  451.                     month = int(match.group(1))
  452.                     day = int(match.group(2))
  453.                     year = int(match.group(3))
  454.                     value = datetime.date(year, month, day)
  455.                 
  456.             
  457.         elif typ == TYPE_HEX:
  458.             
  459.             try:
  460.                 value = int(value, 16)
  461.             except (ValueError, TypeError):
  462.                 log.error('Invalid hex value in .dat file: %s=%s' % (key, value))
  463.                 value = 0
  464.             except:
  465.                 None<EXCEPTION MATCH>(ValueError, TypeError)
  466.             
  467.  
  468.         None<EXCEPTION MATCH>(ValueError, TypeError)
  469.         return value
  470.  
  471.  
  472.